home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / OUTPUT / TRANSPAR.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  14.2 KB  |  440 lines

  1. package sub_arctic.output;
  2.  
  3. import sub_arctic.lib.manager;
  4.  
  5. import java.awt.Graphics;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Color;
  9. import java.awt.Rectangle;
  10. import java.awt.Polygon;
  11. import java.awt.Image;
  12. import java.awt.image.ImageObserver;
  13. import java.awt.image.ImageFilter;
  14. import java.awt.image.RGBImageFilter;
  15. import java.awt.image.ImageProducer;
  16. import java.awt.image.FilteredImageSource;
  17.  
  18. /** 
  19.  * A drawable object that draws everything with increased transparency.  Well,
  20.  * almost everything -- AWT does not support transparent text so text is 
  21.  * always drawn at full opacity.
  22.  *
  23.  * @see java.awt.image.RGBImageFilter
  24.  * @author Scott Hudson
  25.  */
  26. public class transparent_drawable extends drawable {
  27.  
  28.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  29.  
  30.   /** Max transparency that we paint everything with.  225 is opaque, 0 is
  31.    *  fully transparent.
  32.    */
  33.   protected int _alpha_value = 127;
  34.  
  35.   /** Copy of _alpha_value pre-shifted to apply directly as alpha value in
  36.    *  RGB-alpha encoding.
  37.    */
  38.   protected int alpha_mask = (127) << 24;
  39.  
  40.   /** Max transparency that we paint everything with.  225 is opaque, 0 is
  41.    *  fully transparent.
  42.    */
  43.   public int alpha_factor() {return _alpha_value;}
  44.  
  45.   /** Set max transparency that we paint everything with.  225 is opaque, 0 is
  46.    *  fully transparent.
  47.    */
  48.   public void set_alpha_value(int a)
  49.     {
  50.       if (a < 0) a = 0;
  51.       if (a > 255) a = 255;
  52.       _alpha_value = a;
  53.       alpha_mask = a<<24;
  54.     }
  55.  
  56.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  57.  
  58.   /** Filter to increase transparency of images */
  59.   protected ImageFilter trans_filter;
  60.  
  61.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  62.  
  63.   /** 
  64.    * Apply transparency value to a color.  The result has the min of the
  65.    * alpha already in the color and ours.
  66.    *
  67.    * @param Color c the color to be transformed.
  68.    * @return Color the resulting color.
  69.    */
  70.   protected Color apply_alpha(Color c)
  71.     {
  72.       int encoding, alpha;
  73.  
  74.       /* Get the alpha-RGB encoding and strip out the alpha */
  75.       encoding = c.getRGB();
  76.       alpha = (encoding >> 24)& 0xff;
  77.  
  78.       /* use min of our alpha and theirs */
  79.       if (alpha < _alpha_value) 
  80.     return c;
  81.       else
  82.     return new Color((encoding & 0x00ffffff) | alpha_mask);
  83.     }
  84.  
  85.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  86.  
  87.   /** 
  88.    * Full constructor.
  89.    * @param Graphics wrappee the Graphics object we wrap.
  90.    * @param int      alph    the minimum alpha value (0 = fully transparent,
  91.    *                         255 = fully opaque) that this drawable draws in.
  92.    */
  93.   public transparent_drawable(
  94.     Graphics wrappee, 
  95.     int      alph)
  96.     {
  97.       /* let super class initialize based on a copy */
  98.       super(wrappee.create());
  99.  
  100.       /* save our transparency factor */
  101.       set_alpha_value(alph);
  102.  
  103.       /* apply transparency to the current color */
  104.       g.setColor(apply_alpha(g.getColor()));
  105.  
  106.       /* construct a filter for our transparency */
  107.       trans_filter = new transp_filter(_alpha_value);
  108.     }
  109.  
  110.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  111.  
  112.   /** 
  113.    * Constructor that defaults to alpha value of 50% 
  114.    * @param Graphics wrappee the Graphics object we wrap.
  115.    */
  116.   public transparent_drawable(Graphics wrappee)
  117.     {
  118.       this(wrappee, 127);
  119.     }
  120.  
  121.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  122.  
  123.   /** 
  124.    * Full constructor from a drawable.
  125.    * @param drawable wrappee the drawable object we wrap.
  126.    * @param int      alph    the minimum alpha value (0 = fully transparent,
  127.    *                         255 = fully opaque) that this drawable draws in.
  128.    */
  129.   public transparent_drawable(
  130.     drawable wrappee, 
  131.     int      alph)
  132.     {
  133.       /* let super class initialize based on a copy */
  134.       super(wrappee.g.create());
  135.  
  136.       /* save our transparency factor */
  137.       set_alpha_value(alph);
  138.  
  139.       /* apply transparency to the current color */
  140.       g.setColor(apply_alpha(g.getColor()));
  141.  
  142.       /* construct a filter for our transparency */
  143.       trans_filter = new transp_filter(_alpha_value);
  144.     }
  145.  
  146.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  147.  
  148.   /** 
  149.    * Constructor from drawable that defaults to alpha value of 50% 
  150.    * 
  151.    * @param drawable wrappee the drawable object we wrap.
  152.    */
  153.   public transparent_drawable(drawable wrappee)
  154.     {
  155.       this(wrappee, 127);
  156.     }
  157.  
  158.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  159.  
  160.   /** 
  161.    * Override copy() to create a new wrapper also.
  162.    * @return drawable the new transparent_drawable copied from the old.
  163.    */
  164.   public drawable copy() 
  165.     {
  166.       /* create new wrapper */
  167.       return new transparent_drawable(g.create(), _alpha_value);
  168.     }
  169.  
  170.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  171.  
  172.   /** 
  173.    * Override copy() to create a new wrapper also.
  174.    * 
  175.    * @param int x x origin for new drawable.
  176.    * @param int y y origin for new drawable.
  177.    * @param int w width of new drawable.
  178.    * @param int h height of new drawable.
  179.    * @return drawable the new transparent_drawable object based on the old.
  180.    */
  181.   public drawable copy(int x, int y, int w, int h) 
  182.     {
  183.       /* create new wrapper */
  184.       return new transparent_drawable(g.create(x,y,w,h), _alpha_value);
  185.     }
  186.  
  187.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  188.  
  189.   /** 
  190.    * Override create() to create a new wrapper also 
  191.    * @return Graphics the new transparent_drawable copied from the old.
  192.    */
  193.   public Graphics create() 
  194.     {
  195.       /* create new wrapper */
  196.       return new transparent_drawable(g.create(), _alpha_value);
  197.     }
  198.  
  199.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  200.  
  201.   /** 
  202.    * Override create() to create a new wrapper also. 
  203.    * 
  204.    * @param int x x origin for new drawable.
  205.    * @param int y y origin for new drawable.
  206.    * @param int w width of new drawable.
  207.    * @param int h height of new drawable.
  208.    * @return Graphics the new transparent_drawable object based on the old.
  209.    */
  210.   public Graphics create(int x, int y, int w, int h) 
  211.     {
  212.       /* create new wrapper */
  213.       return new transparent_drawable(g.create(x,y,w,h), _alpha_value);
  214.     }
  215.  
  216.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  217.  
  218.   /** 
  219.    * Override to set all colors to one with modified transparency.
  220.    * @param Color c base color we are setting current color to.
  221.    */
  222.   public void setColor(Color c) {g.setColor(apply_alpha(c));}
  223.  
  224.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  225.  
  226.   // note we may not be getting copyArea right, look into that later...
  227.   //      not clear what transparent copyArea should do
  228.  
  229.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  230.  
  231.   /** 
  232.    * Filter the given image to add our transparency to it.
  233.    * @param Image from_img the original image.
  234.    * @result Image the resulting image.
  235.    */
  236.   protected Image make_trans(Image from_img)
  237.     {
  238.       ImageProducer trans_prod;
  239.       Image         result;
  240.  
  241.       trans_prod = new FilteredImageSource(from_img.getSource(), trans_filter);
  242.       result = manager.default_toolkit().createImage(trans_prod);
  243.       manager.wait_for_image(result);
  244.       return result;
  245.     }
  246.  
  247.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  248.  
  249.   /** 
  250.    * Override to filter images, modifying transparency. 
  251.    *
  252.    * @param Image         img      the image to be drawn.
  253.    * @param int           x        x position to place the image at.
  254.    * @param int           y        y position to place the image at.
  255.    * @param ImageObserver observer observer to notify us of progress drawing 
  256.    *                               the Image
  257.    * @return boolean undocumented AWT drawImage return value.
  258.    */
  259.   public boolean drawImage(Image img, int x, int y, ImageObserver observer)
  260.     {
  261.       return g.drawImage(make_trans(img),x,y,observer);
  262.     }
  263.  
  264.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  265.  
  266.   /** 
  267.    * Override to filter images, modifying transparency. 
  268.    *
  269.    * @param Image         img      the image to be drawn.
  270.    * @param int           x        x position to place the image at.
  271.    * @param int           y        y position to place the image at.
  272.    * @param int           w        width that image will be scaled to.
  273.    * @param int           h        height that image will be scaled to.
  274.    * @param ImageObserver observer observer to notify us of progress drawing 
  275.    *                               the Image
  276.    * @return boolean undocumented AWT drawImage return value.
  277.    */
  278.   public boolean drawImage(Image img, int x, int y, int w, int h, 
  279.                                                           ImageObserver obs)
  280.     {
  281.       return g.drawImage(make_trans(img),x,y,w,h,obs);
  282.     }
  283.  
  284.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  285.  
  286.   /** 
  287.    * Override to filter images, modifying transparency. 
  288.    *
  289.    * @param Image         img      the image to be drawn.
  290.    * @param int           x        x position to place the image at.
  291.    * @param int           y        y position to place the image at.
  292.    * @param Color         bgcolor  background color.
  293.    * @param ImageObserver observer observer to notify us of progress drawing 
  294.    *                               the Image
  295.    * @return boolean undocumented AWT drawImage return value.
  296.    */
  297.   public boolean drawImage(Image img, int x, int y, Color bgcolor,
  298.                                           ImageObserver obs) 
  299.     {
  300.       return g.drawImage(make_trans(img),x,y,bgcolor,obs);
  301.     }
  302.  
  303.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  304.  
  305.   /** 
  306.    * Override to filter images, modifying transparency. 
  307.    *
  308.    * @param Image         img      the image to be drawn.
  309.    * @param int           x        x position to place the image at.
  310.    * @param int           y        y position to place the image at.
  311.    * @param int           w        width that image will be scaled to.
  312.    * @param int           h        height that image will be scaled to.
  313.    * @param Color         bgcolor  background color.
  314.    * @param ImageObserver observer observer to notify us of progress drawing 
  315.    *                               the Image
  316.    * @return boolean undocumented AWT drawImage return value.
  317.    */
  318.   public boolean drawImage(Image img, int x, int y, int w, int h, Color bgcolor,
  319.                                           ImageObserver obs)
  320.     {
  321.       return g.drawImage(make_trans(img),x,y,w,h,bgcolor,obs);
  322.     }
  323.  
  324.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  325.  
  326.   /** 
  327.    * Override tiled image drawing, modifying transparency. 
  328.    * @param loaded_image  pattern the pattern we tile with.
  329.    * @param int           x        x position to place the tiling at.
  330.    * @param int           y        y position to place the tiling at.
  331.    * @param int           w        width of tiled area.
  332.    * @param int           h        height of tiled area.
  333.    * @return boolean undocumented AWT drawImage return value from the last draw.
  334.    */
  335.   public boolean tileImage(loaded_image pattern, int x, int y, int w, int h)
  336.     {
  337.       Image img  = pattern.image();
  338.       int size_x = pattern.width();
  339.       int size_y = pattern.height();
  340.       int xpt, ypt;
  341.       Graphics clipped_g;
  342.       boolean v = true;
  343.  
  344.       /* Build a transparency modified copy of the pattern */
  345.       Image trans_pat = make_trans(pattern.image());
  346.  
  347.       /* Build a copy of our Graphics object that clips to exactly the size
  348.        * we are clipping into 
  349.        */
  350.       clipped_g = g.create();
  351.       clipped_g.clipRect(x,y,w,h);
  352.  
  353.       /* loop over image area drawing tiles until we have filled it */
  354.       for (ypt = y; ypt-y < h; ypt += size_y)
  355.         for (xpt = x; xpt-x < w; xpt += size_x)
  356.       v = clipped_g.drawImage(trans_pat, xpt, ypt, _ignore);
  357.  
  358.       /* return the last return value we got */
  359.       return v;
  360.     }
  361.  
  362.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  363. }
  364.  
  365. /*---------------------------------------------------------------------*/
  366.  
  367. /** 
  368.  * An image filter class to modify transparency of each pixel to be the min 
  369.  * of the original value and a given value.
  370.  *
  371.  * @see sub_arctic.output.transparent_drawable
  372.  * @see java.awt.image.RGBImageFilter
  373.  * @author Scott Hudson
  374.  */
  375. class transp_filter extends RGBImageFilter {
  376.  
  377.   /** The transparency value we use.  255 is opaque, 0 is fully transparent. */
  378.   protected int alpha_value;
  379.  
  380.   /** trasnp_factor pre-shifted for use directly in RBG-alpha encoding. */
  381.   protected int alpha_mask;
  382.  
  383.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  384.  
  385.   /** 
  386.    * Constructor.
  387.    * @param int alpha minimum alpha (transparency) value (0 = fully transparent,
  388.    *                  255 = fully opaque).
  389.    */
  390.   public transp_filter(int alph)
  391.     {
  392.       /* flag for the superclass */
  393.       canFilterIndexColorModel = true;
  394.  
  395.       alpha_value = alph;
  396.       alpha_mask  = alph << 24;
  397.     }
  398.  
  399.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  400.  
  401.   /** 
  402.    * The actual filter that processes each pixel.
  403.    * @param int x   the x position of the pixel (we ignore this). 
  404.    * @param int y   the y position of the pixel (we ignore this). 
  405.    * @param int rgb the RGB+Alpha encoding of the pixel's color.
  406.    * @return int the resulting color's encoded value.
  407.    */
  408.   public int filterRGB(int x, int y, int rgb)
  409.     {
  410.       int alpha;
  411.  
  412.       /* strip out the alpha */
  413.       alpha = (rgb >> 24) & 0xff;
  414.  
  415.       /* use min of our alpha and theirs */
  416.       if (alpha < alpha_value) 
  417.     return rgb;
  418.       else
  419.     return (rgb & 0x00ffffff) | alpha_mask;
  420.     }
  421.  
  422.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  423. }
  424. /*=========================== COPYRIGHT NOTICE ===========================
  425.  
  426. This file is part of the subArctic user interface toolkit.
  427.  
  428. Copyright (c) 1996 Scott Hudson and Ian Smith
  429. All rights reserved.
  430.  
  431. The subArctic system is freely available for most uses under the terms
  432. and conditions described in 
  433.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  434. and appearing in full in the lib/interactor.java source file.
  435.  
  436. The current release and additional information about this software can be 
  437. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  438.  
  439. ========================================================================*/
  440.